package com.xdhl.fu.fim.xxljob.job;

import com.alibaba.fastjson.JSONObject;
import com.xdhl.fu.common.core.task.IJob;
import com.xdhl.fu.common.redis.service.RedisService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 节假日更新任务
 *
 * @author 朱力维
 * @time 2022/6/27 13:50
 */
@Component
public class HolidaysUpdateJob implements IJob {

    private static final Logger log = LoggerFactory.getLogger(HolidaysUpdateJob.class);

    public static final String CACHE_KEY = "holidays-2022";

    @Resource
    private RedisService redisService;

    @XxlJob(value = "holidaysUpdateJob", init = "init", destroy = "destroy")
    public ReturnT<String> execute(String param) {
        try {
            this.start();
            log.info("节假日更新成功。");
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error("节假日更新失败。" + e.getMessage());
            return ReturnT.FAIL;
        }
    }

    @Override
    public void start() throws Exception {
        // TODO 待优化为自动从网络上获取
        JSONObject data = new JSONObject();

        Set<String> holidays = new HashSet<>();
        holidays.add("2022-01-01");
        holidays.add("2022-01-02");
        holidays.add("2022-01-03");
        holidays.add("2022-01-31");
        holidays.add("2022-02-01");
        holidays.add("2022-02-02");
        holidays.add("2022-02-03");
        holidays.add("2022-02-04");
        holidays.add("2022-02-05");
        holidays.add("2022-02-06");
        holidays.add("2022-04-03");
        holidays.add("2022-04-04");
        holidays.add("2022-04-05");
        holidays.add("2022-04-30");
        holidays.add("2022-05-01");
        holidays.add("2022-05-02");
        holidays.add("2022-05-03");
        holidays.add("2022-05-04");
        holidays.add("2022-06-03");
        holidays.add("2022-06-04");
        holidays.add("2022-06-05");
        holidays.add("2022-09-10");
        holidays.add("2022-09-11");
        holidays.add("2022-09-12");
        holidays.add("2022-10-01");
        holidays.add("2022-10-02");
        holidays.add("2022-10-03");
        holidays.add("2022-10-04");
        holidays.add("2022-10-05");
        holidays.add("2022-10-06");
        holidays.add("2022-10-07");
        data.put("holidays", holidays.toArray());
        redisService.setCacheObject(CACHE_KEY, data);
    }

    @Override
    public void init() {

    }

    @Override
    public void destroy() {

    }
}
